package com.xian.eduservice.controller.front;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xian.eduservice.entities.Course;
import com.xian.eduservice.entities.Teacher;
import com.xian.eduservice.service.CourseService;
import com.xian.eduservice.service.TeacherService;
import com.xian.entities.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前台数据显示
 * </p>
 *
 * @author xian
 * @since 2022/05/16 18:47
 */
@CrossOrigin
@RestController
@RequestMapping("/edu/indexfront")
public class IndexFrontController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherService teacherService;
    @GetMapping("/index")
    public R index() {
        List<Course> courseList = getHotCourses();
        List<Teacher> teacherList = getHotTeachers();
        return R.ok().data("course",courseList).data("teacher",teacherList);
    }

    @Cacheable(value = "hotTeachers", key ="'getHotTeachers'")
    public List<Teacher> getHotTeachers() {
        // 查询前4条名师
        QueryWrapper<Teacher> hotTeacherWrapper = new QueryWrapper<Teacher>().orderByDesc("sort").last("limit 0,4");
        return teacherService.list(hotTeacherWrapper);
    }

    @Cacheable(value = "hotCourses", key ="'getHotCourses'")
    public List<Course> getHotCourses() {
        // 查询前8条热门课程
        QueryWrapper<Course> hotCourseWrapper = new QueryWrapper<Course>().eq("status","Normal").orderByDesc("view_count").last("limit 0,8");
        return courseService.list(hotCourseWrapper);
    }
}
